// 引入axios
import axios from "axios";

// 配置公共接口
axios.defaults.baseURL = '/api';
// 超时时间
axios.defaults.timeout = 5000;

// 请求拦截器
axios.interceptors.request.use( config => {
  if ( localStorage.token ) {
    config.headers.Authorization = JSON.parse(localStorage.token);
  }
  return config;
}, error => {
  return Promise.reject(error);
} )
// 响应拦截器
axios.interceptors.response.use( response => {
  return response.data;
}, error => {
  return Promise.reject(error);
} )

// 导出get方法
export const get = ( url, data = {} ) => {
  return new Promise( ( resolve, reject ) => {
    axios.get( url, { params: data } ).then( res => resolve( res ),
      err => reject( err ) );
  })
}
// 导出post方法
export const post = ( url, data = {} ) => {
  return new Promise( ( resolve, reject ) => {
    axios.post( url, data ).then( res => resolve( res ),
      err => reject( err ) );
  })
}
// 导出put方法
export const put = ( url, data  ) => {
  return new Promise( ( resolve, reject ) => {
    axios.put( url, { params: data } ).then( res => resolve( res ),
      err => reject( err ) );
  })
}
// 导出del方法
export const del = ( url, data  ) => {
  return new Promise( ( resolve, reject ) => {
    axios.delete( url + data ).then( res => resolve( res ),
      err => reject( err ) );
  })
}
